#ifndef THREAD_SAFE_VECTOR_H
#define THREAD_SAFE_VECTOR_H

#include <vector>
#include <pthread.h>

using namespace std;

template <typename T>
class Vector : public vector<T>
{
	pthread_mutex_t lock;
	
public:
	Vector();
	
	void erase(typename vector<T>::iterator, typename vector<T>::iterator);
	void push_back(const T&);
	T pop_back();
};

template <typename T>
Vector<T>::Vector()
{
	pthread_mutex_init(&lock, NULL);
}

template <typename T>
T Vector<T>::pop_back()
{
	pthread_mutex_lock(&lock);
	T tmp = vector<T>::pop_back();
	pthread_mutex_unlock(&lock);
	
	return tmp;
}

template <typename T>
void Vector<T>::push_back(const T& item)
{
	pthread_mutex_lock(&lock);
	vector<T>::push_back(item);
	pthread_mutex_unlock(&lock);
}

template <typename T>
void Vector<T>::erase(typename vector<T>::iterator begin, typename vector<T>::iterator end)
{
	pthread_mutex_lock(&lock);
	vector<T>::erase(begin, end);
	pthread_mutex_unlock(&lock);
}

#endif
